from selenium import webdriver
import time


driver = webdriver.Chrome()


def get_each_page_info(link):
    # 在虚拟浏览器中打开 Airbnb 页面
    driver.get(link)
    time.sleep(2)
    # 找到当前页所有的出租房信息
    house_list = driver.find_elements_by_css_selector('div._gig1e7')
    for each_house in house_list:
        # 价格
        price = each_house.find_element_by_css_selector('span._j1kt73').text.replace("每晚", "").replace("价格", "")
        # 评价数
        try:
            comment_num = each_house.find_element_by_css_selector('span._69pvqtq').text
        except:
            comment_num = 0
        # 名称
        name = each_house.find_element_by_css_selector('div._qrfr9x5').text
        # 房屋类型、床数量
        category_bed = each_house.find_element_by_css_selector('span._faldii7').text
        category = category_bed.split(' · ')[0]
        bed_num = category_bed.split(' · ')[1]
        print(price, comment_num, name, category, bed_num)


for i in range(0, 5):
    link = 'https://zh.airbnb.com/s/Shenzhen--China/homes?map_toggle=false&items_offset=' + str(i*20)
    print('第'+str(i+1)+"页：")
    get_each_page_info(link)
    time.sleep(2)